.TH std::ranges::ref_view 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::ref_view \- std::ranges::ref_view

.SH Synopsis
   Defined in header <ranges>
   template< ranges::range R >

       requires std::is_object_v<R>                  \fI(since C++20)\fP
   class ref_view

       : public ranges::view_interface<ref_view<R>>

   ref_view is a view of the elements of some other range. It wraps a reference to that
   range.

.SH Member functions

   constructor   constructs a ref_view that references to the given range
   (C++20)       \fI(public member function)\fP
   base          returns the references to the referenced range
   (C++20)       \fI(public member function)\fP
   begin         returns the beginning iterator of the referenced range
   (C++20)       \fI(public member function)\fP
   end           returns the sentinel of the referenced range
   (C++20)       \fI(public member function)\fP
   empty         checks whether the referenced range is empty
   (C++20)       \fI(public member function)\fP
   size          returns the size of the referenced sized_range
   (C++20)       \fI(public member function)\fP
   data          returns the pointer to the beginning of the referenced
   (C++20)       contiguous_range
                 \fI(public member function)\fP
         Inherited from std::ranges::view_interface
   cbegin        returns a constant iterator to the beginning of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   cend          returns a sentinel for the constant iterator of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   operator bool returns whether the derived view is not empty. Provided if
   (C++20)       ranges::empty is applicable to it.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   front         returns the first element in the derived view. Provided if it
   (C++20)       satisfies forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   back          returns the last element in the derived view. Provided if it satisfies
   (C++20)       bidirectional_range and common_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   operator[]    returns the n^th element in the derived view. Provided if it satisfies
   (C++20)       random_access_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP

std::ranges::ref_view::ref_view

   template< /*different-from*/<ref_view> T >

       requires std::convertible_to<T, R&> && requires {                  \fI(since C++20)\fP
   _FUN(std::declval<T>()); }

   constexpr ref_view( T&& t );

   Initializes r_ with std::addressof(static_cast<R&>(std::forward<T>(t))).

   /*different-from*/<T, U> is satisfied if and only if std::remove_cvref_t<T> and
   std::remove_cvref_t<U> are not the same type, and overloads of _FUN are declared as
   void _FUN(R&); void _FUN(R&&) = delete;.

.SH Parameters

   t - range to reference

std::ranges::ref_view::base

   constexpr R& base() const;  \fI(since C++20)\fP

   Equivalent to return *r_;.

std::ranges::ref_view::begin

   constexpr ranges::iterator_t<R> begin() const;  \fI(since C++20)\fP

   Equivalent to return ranges::begin(*r_);.

std::ranges::ref_view::end

   constexpr ranges::sentinel_t<R> end() const;  \fI(since C++20)\fP

   Equivalent to return ranges::end(*r_);.

std::ranges::ref_view::empty

   constexpr bool empty() const                    \fI(since C++20)\fP
       requires requires { ranges::empty(*r_); };

   Equivalent to return ranges::empty(*r_);.

std::ranges::ref_view::size

   constexpr auto size() const           \fI(since C++20)\fP
       requires ranges::sized_range<R>;

   Equivalent to return ranges::size(*r_);.

std::ranges::ref_view::data

   constexpr auto data() const                \fI(since C++20)\fP
       requires ranges::contiguous_range<R>;

   Equivalent to return ranges::data(*r_);.

   Deduction guides

   template< class R >             \fI(since C++20)\fP
   ref_view( R& ) -> ref_view<R>;

   Helper templates

   template< class T >
   inline constexpr bool enable_borrowed_range<ranges::ref_view<T>> =     \fI(since C++20)\fP
   true;

   This specialization of std::ranges::enable_borrowed_range makes ref_view satisfy
   borrowed_range.

.SH Example


// Run this code

 #include <iostream>
 #include <ranges>

 int main()
 {
     const std::string s{"cosmos"};

     const std::ranges::take_view tv{s, 3};
     const std::ranges::ref_view rv{tv};

     std::cout
         << std::boolalpha
         << "call empty() : " << rv.empty() << '\\n'
         << "call size()  : " << rv.size() << '\\n'
         << "call begin() : " << *rv.begin() << '\\n'
         << "call end()   : " << *(rv.end() - 1) << '\\n'
         << "call data()  : " << rv.data() << '\\n'
         << "call base()  : " << rv.base().size() << '\\n' // ~> tv.size()
         << "range-for    : ";

     for (const auto c : rv)
         std::cout << c;
     std::cout << '\\n';
 }

.SH Output:

 call empty() : false
 call size()  : 3
 call begin() : c
 call end()   : s
 call data()  : cosmos
 call base()  : 3
 range-for    : cos

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to          Behavior as published              Correct behavior
                      default constructor was provided as     removed along with the
   P2325R3 C++20      view                                    requirement
                      must be default_initializable

.SH See also

   reference_wrapper   CopyConstructible and CopyAssignable reference wrapper
   \fI(C++11)\fP             \fI(class template)\fP
   ranges::owning_view a view with unique ownership of some range
   (C++20)             \fI(class template)\fP
   views::all_t        a view that includes all elements of a range
   views::all          (alias template) (range adaptor object)
   (C++20)
